package com.jichangxiu.generator.action;

import cn.hutool.core.collection.ListUtil;
import com.jichangxiu.common.annotation.*;
import com.jichangxiu.common.entity.vo.PageResult;
import com.jichangxiu.framework.action.BaseAction;
import com.jichangxiu.generator.entity.dto.add.AddGenCategory;
import com.jichangxiu.generator.entity.dto.edit.EditGenCategory;
import com.jichangxiu.generator.entity.dto.query.QueryGenCategory;
import com.jichangxiu.generator.entity.vo.GenCategoryVo;
import com.jichangxiu.generator.service.GenCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;

/**
 * GenCategoryAction:
 *
 * @author JiChangXiu
 * @create 2024-06-30 13:30:59
 */
@Slf4j
@Rest("/generator/category")
@Tag(name = "生成分类控制器")
public class GenCategoryAction extends BaseAction {

    @Resource
    private GenCategoryService genCategoryService;

    /**
     * 新增生成分类
     *
     * @param addGenCategory 新增生成分类视图对象
     * @return 是否成功
     */
    @Post
    @Limit
    @Repeat
    @Log(remark = "新增生成分类")
    @Operation(summary = "新增生成分类")
    @PreAuthorize("@jcx.hasPower('generator:category:add')")
    public String add(@RequestBody AddGenCategory addGenCategory) {
        return str(genCategoryService.add(addGenCategory));
    }

    /**
     * 单（多）选删除生成分类
     *
     * @param ids 生成分类主键列表
     * @return 是否成功
     */
    @Delete
    @Limit
    @Repeat
    @Log(remark = "单（多）选删除生成分类")
    @Operation(summary = "单（多）选删除生成分类")
    @PreAuthorize("@jcx.hasPower('generator:category:del')")
    public String del(@RequestBody String[] ids) {
        return str(genCategoryService.del(ListUtil.toList(ids)));
    }

    /**
     * 修改生成分类
     *
     * @param editGenCategory 修改生成分类视图对象
     * @return 是否成功
     */
    @Put
    @Limit
    @Repeat
    @Log(remark = "修改生成分类")
    @Operation(summary = "修改生成分类")
    @PreAuthorize("@jcx.hasPower('generator:category:edit')")
    public String edit(@RequestBody EditGenCategory editGenCategory) {
        return str(genCategoryService.edit(editGenCategory));
    }

    /**
     * 根据主键查询生成分类
     *
     * @param id       生成分类主键
     * @param isExtend 是否扩展
     * @return 生成分类详情
     */
    @Get("/{id}")
    @Operation(summary = "根据主键查询生成分类")
    @PreAuthorize("@jcx.hasPower('generator:category:info')")
    public GenCategoryVo info(@PathVariable("id") @NotBlank(message = "主键不能为空") String id, @RequestParam(value = "isExtend", required = false, defaultValue = "false") Boolean isExtend) {
        return genCategoryService.info(id, isExtend);
    }

    /**
     * （可分页）查询生成分类列表
     *
     * @param queryGenCategory 查询视图对象
     * @return （分页）生成分类列表
     */
    @Get("/lists")
    @Operation(summary = "（可分页）查询生成分类列表")
    @PreAuthorize("@jcx.hasPower('generator:category:lists')")
    public PageResult lists(@ParameterObject QueryGenCategory queryGenCategory) {
        return pageResult(genCategoryService.lists(queryGenCategory));
    }

}